#include <stdlib.h>
#include <stdio.h>
#include "dlist.h"


#define THRNUM 50

void * func(void * head)
{
	struct list_head * tmp=(struct list_head *)head;
	struct list_head * node = malloc(sizeof(struct list_head));
//	List_head_init(node);
	List_add_prev(node,head);
	pthread_exit(NULL);
}

struct Testdata
{
	struct list_head list_node;
	int i;
};

/*
 *递归快排序
 * */
void quicksort(struct Testdata *left,struct Testdata *right,struct list_head *head)
{
	struct Testdata *l=left,*r=right;
	if(l==r)
		return;
	struct Testdata tmp=*l;
	printf("tmp-i==%d\t",tmp.i);
	printf("l-i==%d\t",l->i);
	printf("r-i==%d\n",r->i);
//	int j=0;
//	while(j<5i)
	while(&r->list_node!=&l->list_node)
	{	
//		j++;
		//sleep(2);
		printf("tmp-i==%d\n",tmp.i);
		printf("r==begin\t");
		puts("");
		while(r!=NULL&& &(r->list_node)!=head&&r!=l)
		{
			pthread_rwlock_t tmp_rwlock = r->list_node.rwlock;
			pthread_rwlock_wrlock(&tmp_rwlock);
			if(tmp.i <= r->i)
			{
				r=list_entry(r->list_node.prev,struct Testdata,list_node);
				pthread_rwlock_unlock(&tmp_rwlock);
			}
			else
			{
				printf("r-i==%d \t",r->i);
				pthread_rwlock_wrlock(&l->list_node.rwlock);
				l->i=r->i;//将小值向前附
				printf("l-i==%d \t",l->i);
				pthread_rwlock_unlock(&l->list_node.rwlock);
				pthread_rwlock_unlock(&tmp_rwlock);
				break;
			}
		}

		printf("l==begin\t");
		puts("");
		while(l!=NULL&&r!=l&& &(l->list_node)!=head)
		{
			pthread_rwlock_t tmp_rwlock = l->list_node.rwlock;
			pthread_rwlock_wrlock(&tmp_rwlock);
			if(tmp.i>=l->i)
			{
				l=list_entry(l->list_node.next,struct Testdata,list_node);
				pthread_rwlock_unlock(&tmp_rwlock);
			}
			else
			{
				printf("l-i==%d \n",l->i);
				pthread_rwlock_wrlock(&r->list_node.rwlock);
				r->i=l->i;//将大值向后赋
				pthread_rwlock_unlock(&r->list_node.rwlock);
				pthread_rwlock_unlock(&tmp_rwlock);
				break;
			}
		}
	}
	
	//r==l
	pthread_rwlock_wrlock(&l->list_node.rwlock);
	r->i=tmp.i;
	pthread_rwlock_unlock(&l->list_node.rwlock);
/*
	if(tmp.list_node.prev!=head)
	{
		quicksort(list_entry(head->next,struct Testdata,list_node),list_entry(tmp.list_node.prev,struct Testdata,list_node),head);
	}
	if(tmp.list_node.next!=head)
	{
		quicksort(list_entry(tmp.list_node.next,struct Testdata,list_node),list_entry(head->prev,struct Testdata,list_node),head);
	}
*/	
}
int main()
{

/*
	struct list_head *head = malloc(sizeof(struct list_head));
	List_head_init(head);
	int i=0;
	pthread_t tid[THRNUM];
	while(i<THRNUM)
	{
//		pthread_t tmp;
		pthread_create(tid+i,NULL,func,head);
		i++;
	}
	for(i=0;i<THRNUM;i++)
	{
		pthread_join(tid[i],NULL);
	}
	List_destroy(head);
*/

//	struct Testdata * datahead=malloc(sizeof(struct Testdata));
//	List_head_init(&datahead->list_node);
//	datahead->i=0;
//	printf("i=%d sizeofData=%d sizeofListhead=%d\n",datahead->i,sizeof(struct Testdata),sizeof(struct list_head));
	struct list_head * cur;
	struct list_head *head = malloc(sizeof(struct list_head));
	List_head_init(head);
	int i=0;
	for(i=0;i<50;i++)
	{
		struct Testdata * datanode=malloc(sizeof(struct Testdata));
		datanode->i=i+(rand()%100);
		List_add_tail(&datanode->list_node,head);
	}

//上锁？
	struct Testdata * datatmp;
	__list_for_each(cur,head)
	{
		datatmp = list_entry(cur,struct Testdata,list_node);
		printf("i==%d\n",datatmp->i);
	}
	
	quicksort(list_entry(head->next,struct Testdata,list_node),list_entry(head->prev,struct Testdata,list_node),head);
	
	__list_for_each(cur,head)
	{
		datatmp = list_entry(cur,struct Testdata,list_node);
		printf("i2222==%d\n",datatmp->i);
	}
	
	exit(0);
}
